package com.lcxw.hxzbapi.controller.roomcontroller;

import com.alibaba.fastjson.JSONObject;
import com.lcxw.hxzbcommon.entity.ErrorCode;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lcxw.hxzbapi.controller.membercontroller.ImgPicController;
import com.lcxw.hxzbapi.entity.pojo.YwpdRoom;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbapi.service.YwpdRoomService;
import com.lcxw.hxzbcommon.common.ObsUtil;
import com.lcxw.hxzbcommon.common.StringUtil;

/**
 * @author lcwx
 */
@RestController
@RequestMapping("/api/room")
@CrossOrigin
@Validated
public class BakPicUrlController {

    private Logger logger = LoggerFactory.getLogger(ImgPicController.class);

    @Resource
    YwpdRoomService ywpdRoomService;

    private ObsUtil obsUtil = new ObsUtil();

    @PostMapping("/bakPic/set")
    public Response setRoomBakPic(@RequestBody JSONObject requestParams) {

        long roomId = Long.parseLong(requestParams.getString("roomId"));
        long userId = Long.parseLong(requestParams.getString("userId"));

        Response response = new Response();
        YwpdRoom ywpdRoom;

        if (StringUtil.isEmpty(requestParams.getString("bakPic"))) {
            return Response.wrongParam(logger, "bakPic");
        }

        try {
            ywpdRoom = ywpdRoomService.selectRoomByRoomId(roomId);
            if (userId != ywpdRoom.getUserId()) {
                logger.warn("userId {} and roomId {} doesn't match.", userId, roomId);
                response.setCode(ErrorCode.INVALID_REQ);
                response
                    .setDesc("userId {" + userId + "} and roomId {" + roomId + "} doesn't match.");
                return response;
            }
        } catch (Exception e) {
            return Response.dbFail(logger, "select room by roomId");
        }

        String imgOrigin = requestParams.getString("bakPic");
        String imgName = "bakPic/" + roomId + "_bakPic.png";
        String bakPicUrl;
        try {
            bakPicUrl = obsUtil.uploadPic(imgName, imgOrigin);
        } catch (Exception e) {
            return Response.common(logger, "上传背景图片失败", ErrorCode.EXTERNAL_ERROR, "upload bakpic", null);
        }

        logger.info("{} bakPicUrl is generated:{}", roomId, bakPicUrl);

        //将生成的图片URL插入到数据库
        ywpdRoom.setBakPicUrl(bakPicUrl);
        try {
            ywpdRoomService.updateRoom(ywpdRoom);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.dbFail(logger, "set roomBakPic");
        }

        Map<String, String> data = new LinkedHashMap<>();
        data.put("bakPicUrl", bakPicUrl);

        return Response.ok(data);
    }


    @PostMapping("/bakPic/get")
    public Response getUserImgUrl(@RequestBody JSONObject requestParams) {
        logger.info("user {} want to get room {}'s bakPicUrl.", requestParams.getString("userId"),
            requestParams.getString("roomId"));
        if (StringUtil.isEmpty(requestParams.getString("roomId"))) {
            return Response.wrongParam(logger, "roomId");
        }
        Response response = new Response();
        long roomId = Long.parseLong(requestParams.getString("roomId"));

        try {
            String bakPicUrl = ywpdRoomService.selectRoomByRoomId(roomId).getBakPicUrl();
            response.setDesc("get room bakPicUrl success");
            Map<String, String> data = new LinkedHashMap<>();
            data.put("bakPicUrl", bakPicUrl);
            response.setData(data);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.dbFail(logger, "get room {" + roomId + "}'s bakPicUrl");
        }

        return response;
    }
}
